package cn.xx.order.controller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.order.service.IProductOrderService;
import cn.xx.pay.service.IPayBillService;
import cn.xx.product.service.IProductService;
import cn.xx.user.domain.LoginInfo;
import cn.xx.user.domain.User;
import cn.xx.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class ProductOrderController {


    @Autowired
    private IProductOrderService productOrderService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;


    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody Map<String, String> map, HttpServletRequest request, User user) {
        try {
            //获取token
            String token = request.getHeader("TOKEN");
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据id 查询用户
            user = userMapper.findByLoginInfo(loginInfo.getId());
            // 调用创建订单方法
            String result = productOrderService.createOrder(map, user);
            // 返回二维码
            return new AjaxResult().me().setResultObject(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}
